<?php
class km_perakitan_model extends MY_Model {
	var $model = 'km_perakitan_model';
	
	function GetJSON()
	{				//SELECT a.kode_perakitan,a.nama_barang_head,a.margin,a.unit,a.tgl,c.harga_jual 
                    //FROM km_perakitan_head a 
                    //LEFT JOIN km_perakitan_detail c ON a.kode_perakitan = c.kode_perakitan 
	
		$ls_sql	= "	SELECT * 
                    FROM km_perakitan_head   
					WHERE	(kode_perakitan LIKE '%".post('keyword')."%')
					ORDER BY ".(post('sort')=='' ? 'kode_perakitan' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('km_perakitan_head', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('km_perakitan_head', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('km_perakitan_head', array('kode_perakitan' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_pkmaster)
	{
		$result	= $this->db->query("SELECT *
                    				FROM km_perakitan_head
                           			JOIN kode_perakitan c ON a.kode_perakitan = c.kode_perakitan
									WHERE	a.kode_perakitan = '".$as_pkmaster."'");
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('km_perakitan_detail', $arr_rule);
			$this->db->delete('km_perakitan_head', $arr_rule);	
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>